package com.jnu.myaccount_drawer.ui.mainPage;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.myaccount_drawer.R;
import com.jnu.myaccount_drawer.data.AccountType;
import com.jnu.myaccount_drawer.data.DataBank;
import com.jnu.myaccount_drawer.ui.manage.DetailActivity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

public class MainPageFragment extends Fragment {
    public static final int RESULT_CODE_ADD_DATA = 996;
    private MyRecyclerViewAdapter recyclerViewAdapter;
    private List<AccountType> accountType;
    private List<AccountType> accountTypeToday;
    private DataBank dataBank;
    private double grossIncomeToday=0;
    private double grossOutcomeToday=0;
    private double grossIncomeAll=0;
    private double grossOutcomeAll=0;
    private TextView textViewIncomePrice;
    private TextView textViewOutcomePrice;
    private TextView textViewNetAssetsPricePrice;
    private TextView textViewIncomeToday;
    private TextView textViewOutcomeToday;

    Calendar calendar = Calendar.getInstance();
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String stringDate = simpleDateFormat.format(calendar.getTime());

    ActivityResultLauncher<Intent> launcherDetail = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {

    });

    public MainPageFragment() {
        // Required empty public constructor
    }

    public static MainPageFragment newInstance() {
        MainPageFragment fragment = new MainPageFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView=inflater.inflate(R.layout.fragment_main_page, container, false);
        initData();

        //今日日期
        TextView textViewDateTodayText=rootView.findViewById(R.id.text_view_date_today);
        textViewDateTodayText.setText(stringDate);

        //今日收支
        textViewIncomeToday = rootView.findViewById(R.id.text_view_income_today);
        textViewOutcomeToday = rootView.findViewById(R.id.text_view_outcome_today);
        int i;
        for(i =0; i <accountType.size(); i++)
        {
            if(stringDate.equals(accountType.get(i).getDate()))//今日收支
            {
                if(accountType.get(i).getPrice()>=0)
                {
                    grossIncomeToday+=accountType.get(i).getPrice();//今日收入
                }
                else
                {
                    grossOutcomeToday-=accountType.get(i).getPrice();//今日支出
                }
            }
        }
        textViewIncomeToday.setText(grossIncomeToday+"");
        textViewOutcomeToday.setText(grossOutcomeToday+"");

        //总收支
        textViewIncomePrice = rootView.findViewById(R.id.text_view_income_price);
        textViewOutcomePrice = rootView.findViewById(R.id.text_view_outcome_price);
        textViewNetAssetsPricePrice = rootView.findViewById(R.id.text_view_net_assets_price);
        for(i =0; i <accountType.size(); i++)
        {
            if(accountType.get(i).getPrice()>=0)
            {
                grossIncomeAll+=accountType.get(i).getPrice();//总收入
            }
            else
            {
                grossOutcomeAll-=accountType.get(i).getPrice();//总支出
            }
        }
        double grossAccount = grossIncomeAll - grossOutcomeAll;
        textViewIncomePrice.setText(grossIncomeAll+"");
        textViewOutcomePrice.setText(grossOutcomeAll+"");
        textViewNetAssetsPricePrice.setText(grossAccount +"");

        RecyclerView mainRecyclerView=rootView.findViewById(R.id.recycler_view_items_today);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
        mainRecyclerView.setLayoutManager(layoutManager);

        recyclerViewAdapter = new MyRecyclerViewAdapter(accountTypeToday);
        mainRecyclerView.setAdapter(recyclerViewAdapter);
        return rootView;
    }

    public void initData(){
        dataBank = new DataBank(this.getContext());
        accountType=dataBank.loadData();//所有账目
        accountTypeToday= dataBank.loadData();//今日账目
        for(int i=0;i<accountTypeToday.size();i++) {
            if(!accountTypeToday.get(i).getDate().equals(stringDate)) {
                accountTypeToday.remove(i);
                i--;
            }
        }
    }

    private class MyRecyclerViewAdapter extends RecyclerView.Adapter {
        private List<AccountType> accountTypeToday;

        public MyRecyclerViewAdapter(List<AccountType> accountTypeToday) {
            this.accountTypeToday = accountTypeToday;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.account_holder_today, parent, false);

            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, int position) {
            MyViewHolder holder = (MyViewHolder) Holder;
            holder.getImageView().setImageResource(accountTypeToday.get(position).getPictureId());
            holder.getTextViewType().setText(accountTypeToday.get(position).getType());
            holder.getTextViewPrice().setText(accountTypeToday.get(position).getPrice() + "");
            holder.getTextViewPayType().setText(accountTypeToday.get(position).getPayType());

            holder.itemView.setOnClickListener(view -> {
                Intent intent=new Intent(MainPageFragment.this.getContext(), DetailActivity.class);
                intent.putExtra("position",holder.getAdapterPosition());
                intent.putExtra("pictureId", accountTypeToday.get(holder.getAdapterPosition()).getPictureId());
                intent.putExtra("type", accountTypeToday.get(holder.getAdapterPosition()).getType());
                intent.putExtra("price", accountTypeToday.get(holder.getAdapterPosition()).getPrice());
                intent.putExtra("payOrIncome", accountTypeToday.get(holder.getAdapterPosition()).getPayOrIncome());
                intent.putExtra("payType", accountTypeToday.get(holder.getAdapterPosition()).getPayType());
                intent.putExtra("date", accountTypeToday.get(holder.getAdapterPosition()).getDate());
                launcherDetail.launch(intent);
            });
        }

        @Override
        public int getItemCount() {
            return accountTypeToday.size();
        }

        private class MyViewHolder extends RecyclerView.ViewHolder {
            private static final int CONTEXT_MENU_ID_EDIT = 1;
            private static final int CONTEXT_MENU_ID_DELETE = 2;
            private ImageView imageView;
            private TextView textViewType;
            private TextView textViewPrice;
            private TextView textViewPayType;

            public MyViewHolder(View itemView) {
                super(itemView);

                this.imageView = itemView.findViewById(R.id.image_view_account);
                this.textViewType = itemView.findViewById(R.id.text_view_account_type);
                this.textViewPrice = itemView.findViewById(R.id.text_view_account_price);
                this.textViewPayType = itemView.findViewById(R.id.text_view_pay_type);
            }

            public ImageView getImageView() {
                return imageView;
            }

            public TextView getTextViewType() {
                return textViewType;
            }

            public TextView getTextViewPrice() {
                return textViewPrice;
            }

            public TextView getTextViewPayType() {
                return textViewPayType;
            }
        }
    }
}